<?php
class Data_Model_HocVienMapper extends Data_Model_DataMapperAbstract
{
	protected static $_instance = null;
	// dung de phan trang
	private $_paginator = null;
	
	public function HocvienListDate($thang,$nam)
	{
		$db = $this->getDb();
		$selectHocvien= $db->select()
							->from('tbl_hocvien')
							->where('MONTH(NgayDangKy) = ?',$thang)
							->where('YEAR(NgayDangKy) = ?',$nam);
		$hocviens = $db->fetchAll($selectHocvien);
		$hocvienArray = array();
		foreach($hocviens as $hocvien)
		{
			$hocvienArray[] = $this->_populate($hocvien);
		}
		return $hocvienArray;
	}
	public function searchById($id)
	{
		$db = $this->getDb();
		$selectHocvien= $db->select()
							->from('tbl_hocvien')
							->where('MaHocVien = ?',$id);
		$hocvien = $db->fetchRow($selectHocvien);
		
		/* $chitietkhoahocMapper = Data_Model_ChiTietKhoaHocMapper::getInstance();
		$chitietkhoahoc = $chitietkhoahocMapper->searchByMahocvien($hocvien['MaHocVien']);
		$hocvien['tbl_chitietkhoahoc'] = $chitietkhoahoc; */
		
		/* $danhsachthiMapper = Data_Model_DanhSachThiMapper::getInstance();
		$danhsachthi = $danhsachthiMapper->searchByMahocvien($id);
		$hocvien['tbl_danhsachthi'] = $danhsachthi; */
		
	    /* $chitietbangdiemMapper = Data_Model_ChiTietBangDiemMapper::getInstance();
		$diemthi = $chitietbangdiemMapper->searchByMahocvien($id);
		$hocvien['tbl_chitietbangdiem'] = $diemthi; */
		
		return $this->_populate($hocvien);
	}
	public function hocvienList()
	{
		$db = $this->getDb();
		$selectHocvien= $db->select()
						   ->from('tbl_hocvien');
		$hocviens = $db->fetchAll($selectHocvien);
		
		$hocvienArray = array();
		foreach($hocviens as $hocvien)
		{
			/* $chitietkhoahocMapper = Data_Model_ChiTietKhoaHocMapper::getInstance();
			$chitietkhoahoc = $chitietkhoahocMapper->searchByMahocvien($hocvien['MaHocVien']);
			$hocvien['tbl_chitietkhoahoc'] = $chitietkhoahoc; */
			
			/* $danhsachthiMapper = Data_Model_DanhSachThiMapper::getInstance();
			$danhsachthi = $danhsachthiMapper->searchByMahocvien($hocvien['MaHocVien']);
			$hocvien['tbl_danhsachthi'] = $danhsachthi; */
			
			/* $chitietbangdiemMapper = Data_Model_ChiTietBangDiemMapper::getInstance();
			$diemthi = $chitietbangdiemMapper->searchByMahocvien($hocvien['MaHocVien']);
			$hocvien['tbl_chitietbangdiem'] = $diemthi; */
			
			$hocvienArray[] = $this->_populate($hocvien);
		}
		return $hocvienArray;
	}
	
	// hien thi
	public function _populate($data)
	{
		$obj = new Data_Model_HocVien();
		$obj->setId($data['MaHocVien']);
		$obj->setTenhocvien($data['TenHocVien']);
		$obj->setNamsinh($data['NamSinh']);
		$obj->setDienthoai($data['DienThoai']);
		$obj->setEmail($data['Email']);
		$obj->setNgaydangky($data['NgayDangKy']);
		/* $obj->setChitietkhoahoc($data['tbl_chitietkhoahoc']); */
		/* $obj->setLichthi($data['tbl_danhsachthi']); */
		/* $obj->setDiemthi($data['tbl_chitietbangdiem']); */
		
		return $obj;
	}
	protected function _insert(Data_Model_DomainObjectAbstract $obj){
	
		$db = $this->getDb();
		$data = array(
				'TenHocVien' => $obj->getTenhocvien(),
				'NamSinh' => $obj->getNamsinh(),
				'DienThoai' => $obj->getDienThoai(),
				'Email' => $obj->getEmail(),
				'NgayDangKy' => $obj->getNgaydangky()
				);
		
		$db->insert('tbl_hocvien',$data);
		
	}
	protected function _update(Data_Model_DomainObjectAbstract $obj){
		$db = $this->getDb();
		$data = array(
				'TenHocVien' => $obj->getTenhocvien(),
				'NamSinh' => $obj->getNamsinh(),
				'DienThoai' => $obj->getDienThoai(),
				'Email' => $obj->getEmail(),
				'NgayDangKy' => $obj->getNgaydangky()
		);
		$where = $db->quoteInto('MaHocVien = ?',$obj->getId());
		$db->update('tbl_hocvien',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaHocVien = ?',$id);
		$db->delete('tbl_hocvien',$where);
		
	}
	
	// Goi chinh no
	
	public static function getInstance()
	{
		if(null === self::$_instance)
		{
			self::$_instance = new self();				
		}
		return self::$_instance;
	}
}